package com.tankwar.view.panel;

import com.swak.fx.support.Display;

import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;

/**
 * 场景
 * 
 * @author lifeng
 * @date 2020年4月18日 上午10:02:10
 */
public class ScenePanel extends StackPane {

	private volatile AbstractPanel current;
	private ReadyPanel readyPanel;
	private GamePanel gamePanel;
	private double width;
	private double height;

	/**
	 * 就绪
	 */
	public void onReady() {
		init();
		this.getScene().setOnKeyPressed(e -> keyPressed(e));
		this.getScene().setOnKeyReleased(e -> keyReleased(e));
		this.current.start();
	}

	/**
	 * 初始化
	 */
	private void init() {
		this.width = this.getLayoutBounds().getWidth();
		this.height = this.getLayoutBounds().getHeight();
		this.readyPanel = new ReadyPanel(this, this.width, this.height);
		this.gamePanel = new GamePanel(this, this.width, this.height);
		this.getChildren().add(readyPanel);
		this.current = readyPanel;
	}

	/**
	 * 按键按下
	 * 
	 * @param e
	 */
	private void keyPressed(KeyEvent event) {
		this.current.onKeyPressed(event);
	}

	/**
	 * 按键抬起
	 * 
	 * @param e
	 */
	private void keyReleased(KeyEvent event) {
		this.current.onKeyReleased(event);
	}

	/**
	 * 显示准备页面
	 */
	protected void showReady() {
		this.current.stop();
		Display.runUI(() -> {
			this.getChildren().clear();
			this.getChildren().add(readyPanel);
			this.current = readyPanel;
			this.current.start();
		});
	}

	/**
	 * 显示游戏页面
	 */
	protected void showGame() {
		this.current.stop();
		Display.runUI(() -> {
			this.getChildren().clear();
			this.getChildren().add(gamePanel);
			this.current = gamePanel;
			this.current.start();
		});
	}
}